<?php

class Atendimento_ConsultaController extends Zend_Controller_ActionAquiles{

    public function init() {
        parent::init();
        $this->view->title = 'Pesquisa de Atendimentos.';
    }

    public function indexAction() {
        $this->view->form = new Application_Form_Atendimento_Busca();
        $this->view->segmentos = $this->getSegmentos();
        $this->view->hoje = Zend_Date::now()->get('dd/MM/Y');
    }

    public function listaAction() {

        $dado = $this->buscaDados();
        $labels = array('dt_cadastro', 'dt_exclusao');
        $xml = $this->populaXml($dado, $labels, null);
        die($xml);
    }
    
    public function atendimentosAction(){
        echo Zend_Json::encode(array("erros1" => 'testessss'), Zend_Json::TYPE_OBJECT);
        die();
    }

    public function montatabelaAction(){
        $segmento = $_POST['segmento'];
        $dtInicial = new Zend_Date($_POST['dt_inicial']);
        
        $this->view->horarios = Application_Model_SegmentoAgenda::getInstance()->getHorasBySegmento($segmento);
        
        $this->view->days= $this->getDays();
        $model = Application_Model_Atendimento::getInstance();
        $this->view->atendimentos = $model->lista($dtInicial->get('Y-M-d'),$dtInicial->addDay(5)->get('Y-M-d'),$segmento);
        $this->view->maxAtend = $model->getMaxAtendDiaHora($dtInicial->get('Y-M-d'),$dtInicial->addDay(5)->get('Y-M-d'),$segmento);
        $this->view->maxFunc = $model->maximoAntHora($dtInicial->get('Y-M-d'),$dtInicial->addDay(5)->get('Y-M-d'),$segmento);
        
        $this->_helper->layout()->disableLayout();
    }
    
    
    private function buscaDados() {
        $model = Application_Model_Atendimento::getInstance();

        $page = $this->_getParam("page");
        $rp = $this->_getParam("rp");
        $sortname = $this->_getParam("sortname");
        $sortOrder = $this->_getParam("sortorder");
        $descricao = $this->_getParam("descricao");
        $dtCadastro = $this->_getParam("dt_cadastro");

        return $model->lista($page, $rp, $sortname, $sortOrder, $descricao, $dtCadastro);
    }

    private function getDays(){
        $days = array();
        for ($index = 6; $index <= 10; $index++) {
            $days[]= '0'.$index.'/08/2012';
        }
        
        return $days;
    }
    private function getSegmentos(){
        $lista = Application_Model_Segmento::getInstance()->getList();
        $retorno = '<option value="">Selecione</option>';
        foreach ($lista as $value) {
            $retorno.='<option value="'.$value['codigo'].'">'.$value['descricao'].'</option>';
        }
        return $retorno;
    }
}